package com.example.mylibrary;

import static android.content.ContentValues.TAG;

import android.app.Instrumentation;
import android.util.Log;

import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.uiautomator.By;
import androidx.test.uiautomator.UiDevice;
import androidx.test.uiautomator.UiObject2;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class BarterLogin {
    public static UiDevice device;
    public static Instrumentation instrumentation;

    @Before
    public void first() throws InterruptedException {
        Log.i(TAG, "连接设备");
        instrumentation =
                InstrumentationRegistry.getInstrumentation();
        device = UiDevice.getInstance(instrumentation);
        int width = device.getDisplayWidth();
        int height = device.getDisplayHeight();
        device.pressHome();
        Thread.sleep(2000);
        device.swipe(width - 10, height / 2, 50, height / 2, 10);
        Thread.sleep(2000);
        UiObject2 barter = device.findObject(By.text("Barter"));
        barter.click();
        Thread.sleep(3000);
    }

    //    登录-未注册直接登录
    @Test
    private void login1() throws InterruptedException {
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/et_username")).setText("2019012510");
        Thread.sleep(100);
        device.findObject(By.res("com.leaf.collegeidleapp:id/et_password")).setText("123456");
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/btn_login")).click();
        Thread.sleep(1000);
    }
    @Test
    //    登录-已经注册-账号输入错误
    private void login2() throws InterruptedException {
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/tv_register")).
                click();
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/et_username")).
                setText("2019012510");
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/et_password")).
                setText("123456");
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/et_confirm_password")).
                setText("123456");
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/btn_register")).
                click();
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/et_username")).
                setText("2019012511");
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/et_password")).
                setText("123456");
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/btn_login")).
                click();
        Thread.sleep(1000);
    }

    //    登录-已经注册-登录账号不能输入字母
    @Test
    private void login3() throws InterruptedException {
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/tv_register")).
                click();
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/et_username")).
                setText("2019012510");
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/et_password")).
                setText("123456");
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/et_confirm_password")).
                setText("123456");
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/btn_register")).
                click();
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/et_username")).
                setText("wangyan12");
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/et_password")).
                setText("123456");
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/btn_login")).
                click();
        Thread.sleep(1000);
    }

    // 登录-已经注册-登录账号无法输入汉字
    @Test
    private void login4() throws InterruptedException {
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/tv_register")).
                click();
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/et_username")).
                setText("2019012510");
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/et_password")).
                setText("123456");
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/et_confirm_password")).
                setText("123456");
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/btn_register")).
                click();
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/et_username")).
                setText("王岩12");
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/et_password")).
                setText("123456");
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/btn_login")).
                click();
        Thread.sleep(1000);
    }

    //登录-已经注册-密码输入错误
    @Test
    private void login5() throws InterruptedException {
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/tv_register")).
                click();
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/et_username")).
                setText("2019012510");
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/et_password")).
                setText("123456");
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/et_confirm_password")).
                setText("123456");
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/btn_register")).
                click();
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/et_username")).
                setText("2019012510");
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/et_password")).
                setText("1234567");
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/btn_login")).
                click();
        Thread.sleep(1000);
    }

    //登录-已经注册-网络连接失败
    @Test
    private void login6() throws InterruptedException {
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/tv_register")).
                click();
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/et_username")).
                setText("2019012510");
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/et_password")).
                setText("123456");
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/et_confirm_password")).
                setText("123456");
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/btn_register")).
                click();
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/et_username")).
                setText("2019012510");
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/et_password")).
                setText("123456");
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/btn_login")).
                click();
        Thread.sleep(1000);
    }

    //登录-已经注册-登录成功
    @Test
    private void login7() throws InterruptedException {
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/tv_register")).
                click();
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/et_username")).
                setText("2019012510");
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/et_password")).
                setText("123456");
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/et_confirm_password")).
                setText("123456");
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/btn_register")).
                click();
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/et_username")).
                setText("2019012510");
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/et_password")).
                setText("123456");
        Thread.sleep(1000);
        device.findObject(By.res("com.leaf.collegeidleapp:id/btn_login")).
                click();
        Thread.sleep(1000);
    }

    @After
    public static void tearDown() {
        device.pressBack();
    }
}
